package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentArea;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBasics;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepPlan;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepPlanTarget;
import com.mxpio.erp.common.equipment.service.EquipmentAreaService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpRelCategoryService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanTargetService;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 点巡检计划目标Service业务层处理
 *
 * @author Cheng
 * @date 2023-07-19
 */
@Service
@Slf4j
public class EquipmentUpkeepPlanTargetServiceImpl extends BaseServiceImpl<EquipmentUpkeepPlanTarget> implements EquipmentUpkeepPlanTargetService {

    @Resource
    private EquipmentAreaService equipmentAreaService;

    @Resource
    private EquipmentEqpRelCategoryService equipmentEqpRelCategoryService;

    @Override
    @Transactional
    public Result<?> addAreaTarget(EquipmentUpkeepPlanTarget equipmentUpkeepPlanTarget) {
        // 查询区域信息
        EquipmentArea area = equipmentAreaService.getById(EquipmentArea.class, equipmentUpkeepPlanTarget.getTargetId());
        if (area == null) {
            return Result.error("区域为空！");
        }
        Long count = JpaUtil.linq(EquipmentUpkeepPlanTarget.class)
                .equal("planId", equipmentUpkeepPlanTarget.getPlanId())
                .equal("checkType", equipmentUpkeepPlanTarget.getCheckType())
                .equal("targetId", equipmentUpkeepPlanTarget.getTargetId())
                .count();
        if (count > 0){
            log.info("重复添加：{}",equipmentUpkeepPlanTarget.getTargetId());
            return Result.error("存在重复目标,请重新选择！");
        }
        equipmentUpkeepPlanTarget.setTargetCode(area.getAreaCode());
        if (area.getPid() != null && !"0".equals(area.getPid())) {
            String parentAllName = equipmentAreaService.getParentAllNameByPid(area.getPid());
            equipmentUpkeepPlanTarget.setTargetName(parentAllName +"/"+ area.getAreaName());
        } else {
            equipmentUpkeepPlanTarget.setTargetName(area.getAreaName());
        }
        this.save(equipmentUpkeepPlanTarget);
        return Result.OK("添加成功！");
    }

    @Override
    @Transactional
    public Result<?> addEqpTargets( List<EquipmentUpkeepPlanTarget> targetList) {
        List<EquipmentUpkeepPlanTarget> targets = new ArrayList<>(targetList.size());
        for (EquipmentUpkeepPlanTarget target:targetList){
            Long count = JpaUtil.linq(EquipmentUpkeepPlanTarget.class)
                    .equal("planId", target.getPlanId())
                    //.equal("checkType", target.getCheckType())
                    .equal("targetId", target.getTargetId())
                    .count();
            if (count > 0){
                //throw new MBootException("重复添加："+target.getTargetId());
                return Result.error("重复添加："+target.getTargetId());
            }
            EquipmentUpkeepPlanTarget saveTarget = new EquipmentUpkeepPlanTarget();
            BeanReflectionUtils.copyProperties(saveTarget,target);
            saveTarget.setId(null);
            targets.add(saveTarget);
        }
        save(targets);
        return Result.OK("添加成功！");
    }

    @Override
    @Transactional
    public Result<?> addTargetByCategoryId(String planId, String categoryId) {
        // 查询plan
        EquipmentUpkeepPlan plan = JpaUtil.getOne(EquipmentUpkeepPlan.class, planId);
        if (plan == null) {
            //throw new MBootException("未查询到对应计划数据！");
            return Result.error("未查询到对应计划数据！");
        }

        // 查询出来分类以及其子分类下的所有设备
        List<EquipmentEqpBasics> tpmEqpBasics = equipmentEqpRelCategoryService.listAllEqpByCategoryId(categoryId);

        if (CollectionUtils.isEmpty(tpmEqpBasics)) {
            //throw new MBootException("该分类对应的设备台账为空！");
            return Result.error("该分类对应的设备台账为空！");
        }
        tpmEqpBasics.forEach(eqp -> {
            Long count = JpaUtil.linq(EquipmentUpkeepPlanTarget.class)
                    .equal("planId", planId)
                    //.equal("checkType", plan.getTargetType())
                    .equal("targetId", eqp.getBasicsCode())
                    .count();
            if (count == 0 ){
                EquipmentUpkeepPlanTarget target = new EquipmentUpkeepPlanTarget();
                target.setPlanId(planId);
                target.setCheckType(plan.getTargetType());
                target.setTargetId(eqp.getBasicsCode());
                target.setTargetName(eqp.getEqpName());
                target.setTargetCode(eqp.getEqpCode());
                target.setTargetSpec(eqp.getSpecType());
                target.setUpkeepPersonGroupCode(eqp.getUpkeepPersonGroupCode());
                target.setLastUpkeepDate(eqp.getLastUpkeepDate());
                save(target);
            }
        });

        return Result.OK("添加成功！");
    }

    @Override
    @Transactional
    public List<EquipmentUpkeepPlanTarget> selectByMainId(String mainId) {
        //Criteria c = Criteria.create().addCriterion("planId", Operator.EQ, mainId);
        //return list(EquipmentUpkeepPlanTarget.class,c);
        return JpaUtil.linq(EquipmentUpkeepPlanTarget.class).collect("basicsCode",EquipmentEqpBasics.class,"targetId").equal("planId",mainId).list();
    }
}
